package com.hexun.es.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hexun.es.enums.ResultEnum;
import com.hexun.es.model.Person;
import com.hexun.es.service.EsService;
import com.hexun.es.service.PersonService;
import com.hexun.es.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:ES控制器
 * @author:yrw
 * @date 2018/11/20
 */
@RestController
@RequestMapping("/es")
public class EsController {

    @Autowired
    private EsService esService;

    @Autowired
    private PersonService personService;

    /**
     * 创建索引
     * @param index 索引，类似数据库
     * @return
     */
    @RequestMapping("/createIndex/{index}")
    public String createIndex(@PathVariable String index){
        esService.createIndex(index);
        return ResultEnum.SUCCESS.getDesc();
    }

    /**
     * 删除索引
     * @param index 索引，类似数据库
     * @return
     */
    @RequestMapping("/deleteIndex/{index}")
    public String deleteIndex(@PathVariable String index){
        esService.deleteIndex(index);
        return ResultEnum.SUCCESS.getDesc();
    }

    /**
     * 添加数据
     * @param index 索引，类似数据库
     * @param type 类型，类似表
     * @return
     */
    @RequestMapping("/addData/{index}/{type}")
    public String addData(@PathVariable String index,@PathVariable String type){
        Result result = personService.findAll();
        if(result.Success()){
            List<Person> personList= (List<Person>) result.getData();
            if(personList != null && personList.size() > 0){
                for(Person p:personList){
                    esService.addData(JSONObject.parseObject(JSON.toJSONString(p)),index,type,null);
                }
            }
        }else{
            return ResultEnum.FAIL.getDesc();
        }
        return ResultEnum.SUCCESS.getDesc();
    }

    /**
     * 根据id删除数据
     * @param index 索引，类似数据库
     * @param type 类型，类似表
     * @param id 数据id
     * @return
     */
    @RequestMapping("/deleteById/{index}/{type}/{id}")
    public String deleteById(@PathVariable String index,@PathVariable String type,@PathVariable String id){
        try {
            esService.deleteById(index,type,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultEnum.SUCCESS.getDesc();
    }

    /**
     * 根据id更新数据
     * @param index 索引，类似数据库
     * @param type 类型，类似表
     * @param id 数据id
     * @return
     */
    @RequestMapping("/updateById/{index}/{type}/{id}")
    public String updateById(@PathVariable String index,@PathVariable String type,@PathVariable String id,
                             @NotNull String data){
        try {
            esService.updateById(JSONObject.parseObject(data),index,type,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ResultEnum.SUCCESS.getDesc();
    }

    /**
     * 判断索引是否存在
     * @param index 索引，类似数据库
     * @return
     */
    @RequestMapping("/isIndexExist/{index}")
    public boolean isIndexExist(@PathVariable String index){
        boolean flag;
        try {
            flag = esService.isIndexExist(index);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    /**
     * 根据id查询
     * @param index 索引，类似数据库
     * @param type 类型，类似表
     * @param id 数据id
     * @return
     */
    @RequestMapping("/searchById/{index}/{type}/{id}")
    public String searchById(@PathVariable String index,@PathVariable String type,@PathVariable String id){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            map=esService.searchById(index,type,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询
     * @param pageNo 当前页
     * @param pageSize 一页多少记录
     * @param fieldSort 排序字段
     * @param sortOrder 升序还是降序
     * @param p
     * @return
     */
    @RequestMapping("/searchPerson")
    public String searchPerson(Integer pageNo,Integer pageSize,String fieldSort, String sortOrder, Person p){
        List<Map<String,Object>> mapList;
        try {
            mapList=esService.searchPerson(pageNo, pageSize, fieldSort, sortOrder, p);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return JSON.toJSONString(mapList);
    }
}
